<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\model\CommonModel;
use app\admin\model\PreResultModel;
use app\admin\model\TalkModel;
use app\admin\model\UserModel;
use cmf\controller\AdminBaseController;
use think\Db;

//免费聊吧
class FreeController extends AdminBaseController
{
    public function index(){
        $param = $this->request->param();
        $type = UserModel::EXPERT_TYPE;//足球类型
        //查询专家
        $talk = new TalkController();
        $expert = $talk->getExpert();
        $admin_id = cmf_get_current_admin_id();
        $where['type1'] = TalkModel::TALK_FREE;
        if($admin_id != 1){
            $where['user_id'] = $admin_id;
        }
        $list = Db::name('talk')
            ->where($where)
            ->where(function($query) use($param){
                if(isset($param['title']) && !empty($param['title'])){
                    $title = $param['title'];
                    $query->where('title','like','%'.$title.'%');
                }
                if(isset($param['type_id']) && $param['type_id'] != '-1'){
                    $query->where('type',$param['type_id']);
                }
            })
            ->field('*')
            ->order('id desc')
            ->paginate(null,false,['query'=>$param])
            ->each(function($item, $key) use($type,$expert){
                $item['expert_name'] = '';
                if(isset($expert[$item['user_id']])){
                    $item['expert_name'] = $expert[$item['user_id']];//专家姓名
                }

                $item['type'] = $type[$item['type']];
                return $item;
            });;
        $page = $list->render();
        $this->assign('list', $list);
        $this->assign('page', $page);

        //类型
        $this->assign('type',UserModel::EXPERT_TYPE);
        //搜索类型回显
        $this->assign('type_id',$param['type_id']??-1);

        return $this->fetch();
    }

    //添加页面
    public function add(){
        //类型
        $this->assign('type',UserModel::EXPERT_TYPE);
        return $this->fetch();
    }

    //添加提交
    public function addPost(){
        //管理员身份是否专家
        $talkModel = new TalkModel();
        $admin_id = cmf_get_current_admin_id();
        $expert = $talkModel->isExpert($admin_id);
        if(!$expert){
            $this->error('无权操作');
        }

        $data = $this->request->param();
        $result = $this->validate($data,'Free.add');
        if($result !== true){
            $this->error($result);
        }

        $commonModel = new CommonModel();
        $data['user_id'] = $admin_id;
        $data['content'] = $commonModel->setPostContentAttr($data['content']);
        //过滤data数组中的非数据表字段数据
        $talkModel = new TalkModel();
        $res = $talkModel->allowField(true)->create($data);

        //批量写入预测结果表
        if(isset($data['bat_ids']) && !empty($data['bat_ids'])){
            $talk_id = $res->id;
            $bat_id_s = explode(',',$data['bat_ids']);
            //合并数组
            $key = ['bat_id','main_score','pass_score'];
            $res_result = $commonModel->array_merge_more($key,$bat_id_s,$data['main_score'],$data['pass_score']);
            foreach($res_result as &$value){
                $value['talk_id'] = $talk_id;
            }
            $preModel = new PreResultModel();
            $preModel->saveAll($res_result);
        }

        if($res){
            $this->success('添加成功!', url('Free/index'));
        }else{
            $this->error('失败');
        }
    }

    //编辑页面
    public function edit(){
        $id = $this->request->param('id');
        $post = Db::name('talk')
            ->where(['id'=>$id])
            ->find();
        $commonModel = new CommonModel();
        $post['content'] = $commonModel->getPostContentAttr($post['content']);
        $this->assign('post',$post);

        //查询预测结果表
        $res_result = $commonModel->selectWhereData('pre_result',['talk_id'=>$post['id']],'id,bat_id,main_score,pass_score');
        $bat_ids = array_column($res_result,'bat_id');
        $this->assign('bat_ids',implode(',',$bat_ids));
        //预测比分
        $main_score_ids = array_column($res_result,'main_score');
        $pass_score_ids = array_column($res_result,'pass_score');
        $this->assign('pre_result_main',implode(',',$main_score_ids));
        $this->assign('pre_result_score',implode(',',$pass_score_ids));

        //类型
        $this->assign('type',UserModel::EXPERT_TYPE);
        //聊吧类型
        $this->assign('type1',TalkModel::TALK_TYPE);
        return $this->fetch();
    }

    //编辑提交
    public function editPost(){
        //管理员身份是否专家
        $talkModel = new TalkModel();
        $admin_id = cmf_get_current_admin_id();
        $expert = $talkModel->isExpert($admin_id);
        if(!$expert){
            $this->error('无权操作');
        }

        $data = $this->request->param();
        $result = $this->validate($data,'Free.edit');
        if($result !== true){
            $this->error($result);
        }
        $commonModel = new CommonModel();
        $data['content'] = $commonModel->setPostContentAttr($data['content']);
        // 过滤data数组中的非数据表字段数据
        $res = $talkModel->allowField(true)->save($data,['id'=>$data['id']]);

        //删除预测结果
        if(isset($data['bat_ids']) && !empty($data['bat_ids'])){
            $preModel = new PreResultModel();
            $preModel->where('talk_id',$data['id'])->delete();
            //批量写入预测结果表
            $talk_id = $data['id'];
            $bat_id_s = explode(',',$data['bat_ids']);
            //合并数组
            $key = ['bat_id','main_score','pass_score'];
            $res_result = $commonModel->array_merge_more($key,$bat_id_s,$data['main_score'],$data['pass_score']);
            foreach($res_result as &$value){
                $value['talk_id'] = $talk_id;
            }
            $preModel->saveAll($res_result);
        }

        if($res){
            $this->success('保存成功!', url('Free/index'));
        }else{
            $this->error('保存失败');
        }
    }

    //删除
    public function delete(){
        //管理员身份是否专家
        $admin_id = cmf_get_current_admin_id();
        $talkModel = new TalkModel();
        $expert = $talkModel->isExpert($admin_id);
        if(!$expert){
            $this->error('无权操作');
        }

        $id = $this->request->param('id');
        Db::startTrans();
        $res = $talkModel->where(['id'=>$id])->delete();

        //删除预测结果
        $preModel = new PreResultModel();
        $res1 = $preModel->where('talk_id',$id)->delete();
        if($res && $res1){
            Db::commit();
            $this->success('删除成功！');
        }else{
            Db::rollback();
            $this->error('删除失败！');
        }
    }
}